package per.hx.bs.web.yw.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.BaseController;
import per.hx.bs.base.annotation.BsCLog;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.base.domain.Tree;
import per.hx.bs.entity.yw.YwAppraisalItemDO;
import per.hx.bs.entity.yw.YwAppraisalTypeDO;
import per.hx.bs.web.yw.service.YwAppraisalService;

import java.util.List;

/**
 * @author HuXing
 * @date 2018/4/14  0:32
 */
@Controller
@RequestMapping(value = "yw/appraisal")
public class AppraisalController extends BaseController {

    private final String prefix = "yw/appraisal/";

    private final
    YwAppraisalService appraisalService;

    @Autowired
    public AppraisalController(YwAppraisalService appraisalService) {
        this.appraisalService = appraisalService;
    }

    @BsCLog("进入绩效津贴考核指标界面")
    @RequiresPermissions("yw:zb:zb")
    @GetMapping(value = "/appraisal")
    public String appraisal() {
        return prefix + "appraisal";
    }

    @BsCLog("进入绩效津贴考核指标界面")
    @RequiresPermissions("yw:zb:zb")
    @GetMapping(value = "/typeMange")
    public String typeMange() {
        return prefix + "typeMange";
    }

    @BsCLog("获取绩效津贴考核指标")
    @RequiresPermissions("yw:zb:zb")
    @GetMapping(value = "/list")
    @ResponseBody
    public List<YwAppraisalItemDO> list(@RequestParam("type") String type) {
        return appraisalService.list(type);
    }

    @BsCLog("获取一级绩效津贴考核指标")
    @RequiresPermissions("yw:zb:zb")
    @GetMapping(value = "/type")
    @ResponseBody
    public List<YwAppraisalTypeDO> type() {
        return appraisalService.type();
    }

    @BsCLog("进入一级指标添加界面")
    @RequiresPermissions("yw:zb:add")
    @GetMapping(value = "/type/add")
    public String typeAdd() {
        return prefix + "typeAdd";
    }

    @BsCLog("保存绩效津贴考核一级指标")
    @RequiresPermissions("yw:zb:add")
    @PostMapping(value = "/type/save")
    @ResponseBody
    public ReturnDTO typeSave(YwAppraisalTypeDO typeDO) {
        typeDO.setCreateId(getUserId());
        return ReturnDTO.check(appraisalService.saveType(typeDO));

    }

    @BsCLog("进入一级指标修改界面")
    @RequiresPermissions("yw:zb:edit")
    @GetMapping(value = "/type/edit/{id}")
    public String typeEdit(@PathVariable("id") String id, Model model) {
        YwAppraisalTypeDO appraisalTypeDO = appraisalService.findTypeById(id);
        if (appraisalTypeDO != null) {
            model.addAttribute("type", appraisalTypeDO);
        }
        return prefix + "typeEdit";
    }

    @BsCLog("修改绩效津贴考核一级指标")
    @RequiresPermissions("yw:zb:edit")
    @PostMapping(value = "/type/update")
    @ResponseBody
    public ReturnDTO typeUpdate(YwAppraisalTypeDO typeDO) {
        typeDO.setUpdateId(getUserId());
        if (appraisalService.updateType(typeDO) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error();
        }

    }

    @BsCLog("删除绩效津贴考核一级指标")
    @RequiresPermissions("yw:zb:remove")
    @GetMapping(value = "/type/delete/{id}")
    @ResponseBody
    public ReturnDTO typeDelete(@PathVariable("id") String id) {
        if (appraisalService.deleteType(id) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error();
        }
    }

    @BsCLog("进入二级指标添加界面")
    @RequiresPermissions("yw:zb:add")
    @GetMapping(value = "/item/add/{showId}/{pId}")
    public String itemAdd(@PathVariable("pId") String pId, @PathVariable("showId") String showId, Model model) {

        System.out.println("s--------" + showId);
        model.addAttribute("pId", pId);
        model.addAttribute("showId", showId);
        switch (pId.charAt(0)) {
            case 't':
                YwAppraisalTypeDO typeDO = appraisalService.findTypeById(pId);
                model.addAttribute("pName", typeDO != null ? typeDO.getAppraisalName() : "");
                model.addAttribute("pType", typeDO != null ? typeDO.getAppraisalValue() : "");
                break;
            case 'i':
                YwAppraisalItemDO itemDO = appraisalService.findItemById(pId);
                model.addAttribute("pName", itemDO != null ? itemDO.getAppraisalContent() : "");
                model.addAttribute("pType", itemDO != null ? itemDO.getAppraisalType() : "");
                break;
            default:
                model.addAttribute("pName", "添加错误请重新进入页面");
        }
        return prefix + "itemAdd";
    }

    @BsCLog("保存绩效津贴考核一级指标")
    @RequiresPermissions("yw:zb:add")
    @PostMapping(value = "/item/save")
    @ResponseBody
    public ReturnDTO itemSave(YwAppraisalItemDO itemDO) {
        itemDO.setCreateId(getUserId());
        System.out.println(itemDO);
        return ReturnDTO.check(appraisalService.saveItem(itemDO));
    }

    @BsCLog("进入二级指标修改界面")
    @RequiresPermissions("yw:zb:edit")
    @GetMapping(value = "/item/edit/{id}")
    public String itemEdit(@PathVariable("id") String id, Model model) {
        YwAppraisalItemDO appraisalItemDO = appraisalService.findItemById(id);
        if (appraisalItemDO != null) {
            model.addAttribute("item", appraisalItemDO);
            String pId = appraisalItemDO.getParentId();
            switch (pId.charAt(0)) {
                case 't':
                    YwAppraisalTypeDO typeDO = appraisalService.findTypeById(pId);
                    model.addAttribute("pName", typeDO != null ? typeDO.getAppraisalName() : "");
                    break;
                case 'i':
                    YwAppraisalItemDO itemDO = appraisalService.findItemById(pId);
                    model.addAttribute("pName", itemDO != null ? itemDO.getAppraisalContent() : "");
                    break;
                default:
                    model.addAttribute("pName", "添加错误请重新进入页面");
            }
        }
        return prefix + "itemEdit";
    }

    @BsCLog("修改绩效津贴考核一级指标")
    @RequiresPermissions("yw:zb:edit")
    @PutMapping(value = "/item/edit")
    @ResponseBody
    public ReturnDTO itemUpdate(YwAppraisalItemDO itemDO) {
        itemDO.setUpdateId(getUserId());
        if (appraisalService.updateItem(itemDO) > 0) {
            return ReturnDTO.ok();
        } else {
            return ReturnDTO.error();
        }

    }

    @BsCLog("删除绩效津贴考核二级指标")
    @RequiresPermissions("yw:zb:edit")
    @GetMapping(value = "/item/delete/{id}")
    @ResponseBody
    public ReturnDTO itemDelete(@PathVariable("id") String id) {
        if (appraisalService.itemHasChild(id)) {
            if (appraisalService.deleteItem(id) > 0) {
                return ReturnDTO.ok();
            }
            return ReturnDTO.error();
        }
        return ReturnDTO.error("其下有子指标请，删除后在操作");
    }

    @BsCLog("通过ParentId获取绩效津贴考核二级指标")
    @GetMapping(value = "/item/parentId/{pId}")
    @ResponseBody
    public List<YwAppraisalItemDO> selectByParentId(@PathVariable("pId") String pId) {
        return appraisalService.selectByParentId(pId);
    }

    @BsCLog("进入绩效津贴考核指标树形界面")
    @GetMapping(value = "/appraisalTree")
    public String treeView(String type, Model model) {
        model.addAttribute("type", type);
        return prefix + "appraisalTree";
    }

    @BsCLog("获取树形指标列表")
    @GetMapping(value = "/tree")
    @ResponseBody
    public Tree tree(String type) {
        return JSON.parseObject(appraisalService.getTree(type), Tree.class);
    }

    @BsCLog("通过id获取指标信息")
    @GetMapping(value = "/item/{id}")
    @ResponseBody
    public ReturnDTO getItemOne(@PathVariable("id") String id) {
        return ReturnDTO.ok(appraisalService.findItemById(id));

    }


    @BsCLog("计入统计")
    @GetMapping(value = "/item/countSignIn/{id}")
    @ResponseBody
    public ReturnDTO countSignIn(@PathVariable("id") String id) {
        YwAppraisalItemDO itemDO = new YwAppraisalItemDO();
        itemDO.setId(id);
        itemDO.setCountSign(1);
        return ReturnDTO.ok(appraisalService.updateItem(itemDO));
    }

    @BsCLog("移除统计")
    @GetMapping(value = "/item/countSignOut/{id}")
    @ResponseBody
    public ReturnDTO countSignOut(@PathVariable("id") String id) {

        YwAppraisalItemDO itemDO = new YwAppraisalItemDO();
        itemDO.setId(id);
        itemDO.setCountSign(0);
        return ReturnDTO.ok(appraisalService.updateItem(itemDO));

    }
}
